// house_pkg/pages/locate/index.ts
import QQMap from '../../../utils/qqmap'
Page({
  data: {
    points: [],
  },
  onLoad() {
    this.getLocation()
  },
  async getLocation() {
    const { latitude, longitude } = await wx.getLocation()
    this.getPoint(latitude, longitude)
  },
  async chooseLocation() {
    const { latitude, longitude } = await wx.chooseLocation()
    this.getPoint(latitude, longitude)
  },
  getPoint(latitude, longitude) {
    QQMap.reverseGeocoder({
      location: [latitude, longitude].join(','),
      success: ({ result: { address } }) => {
        this.setData({ address })
      },
    })
    QQMap.search({
      keyword: '小区',
      location: [latitude, longitude].join(','),
      page_size: 5,
      success: (result) => {
        // console.log(result)
        const points = result.data.map(({ id, title, _distance }) => {
          return { id, title, _distance }
        })
        this.setData({ points })
      },
    })
  },
})
